﻿/*
----------------------------------------------------------
* Company Name		: Worldsoft							
* Project Name		: RealState MVC 3.0							
* Object Name		: HtmlHelperExtensions.cs
* Description		: HtmlHelperExtensions Image Extension			
* Programmer		: Nguyen Ngoc Manh
* Creation Date		: 04/12/2010		(dd/mm/yyyy)					
----------------------------------------------------------
 USAGE: <%= Html.Image( Url.Content( "~/Content/images/img.png" ),
               "alt text",
               new { id = "myImage", border = "0" } )
        %>
 */

// BỔ SUNG THÊM VÀO THƯ VIỆN HTMLHELPER THẺ IMAGE
using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using System.Text;
using System.Collections.Generic;

namespace RealEstate.MVC.Web.Helper
{
    public class CheckedList<T> : Dictionary<T, bool> { }

    public static class HtmlHelperExtensions
    {
        public static string Image(this HtmlHelper helper,
                                   string url,
                                   string altText,
                                   object htmlAttributes, string dataToolTip)
        {
            var builder = new TagBuilder("img");
            builder.Attributes.Add("src", url);
            builder.Attributes.Add("alt", altText);
            if (!string.IsNullOrEmpty(dataToolTip)) { builder.Attributes.Add("data-tooltip", dataToolTip); }
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return builder.ToString(TagRenderMode.SelfClosing);
        }

        public static string CheckBoxList<T, TModel, TValue, TText>(this HtmlHelper<TModel> helper, string listName, string valueProperty,
 string textProperty, CheckedList<T> checkedList, Func<T, TValue> value, Func<T, TText> text)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < checkedList.Count; i++)
            {
                T item = checkedList.Keys.ToList()[i];
                builder.AppendLine("<p>");
                builder.AppendLine(helper.Hidden(listName + "[" + i + "].Key." + valueProperty, value(item)).ToHtmlString());
                builder.AppendLine(helper.Hidden(listName + "[" + i + "].Key." + textProperty, text(item)).ToHtmlString());
                builder.AppendLine(helper.CheckBox(listName + "[" + i + "].Value", checkedList[item]).ToHtmlString());
                builder.AppendLine(text(item).ToString());
                builder.AppendLine("</p>");
            }
            return builder.ToString();
        }
    }
}